package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * 登录界面
	 * @author long
	 * @Create  2012-10-3 上午11:47:47
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class LoginMediator extends Mediator
	{
		public static const NAME:String="LoginMediator";

		private var userid:String;

		public function LoginMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			this.mcLogin.txt_userid.text="test";
			this.mcLogin.txt_password.text="1234";
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.LOGINMEDIATOR_RES_LOGIN, CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED, CLZNoticeName.CONNECT_SOCKET_SUCC];
		}

		override public function handleNotification(notification:INotification):void
		{
			var body:Object=notification.getBody();
			switch (notification.getName())
			{
				case CLZNoticeName.LOGINMEDIATOR_RES_LOGIN:
				{
					this.mcLogin.btn_Login.addEventListener(MouseEvent.CLICK, this.onLoginHandler);
					switch (body.type)
					{
						case 0:
						{
							trace("login success!");
							//组织玩家信息
							GameData.getInstance().userInfo.userVo.username=this.userid;
							if (Texas.I._mcTableView.contains(this.mcLogin))
								Texas.I._mcTableView.removeChild(this.mcLogin);
							this.facade.removeMediator(LoginMediator.NAME);
							
//							this.sendNotification(CLZNoticeName.CHATROOMCOMMAND_ENTER_CHATROOM);
							//在进入大厅
							this.sendNotification(CLZNoticeName.LOBBYCOMMAND_ENTER_LOBBY, Texas.I);
//							this.facade.removeMediator(LoginMediator.NAME);
							break;
						}
						case 1:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "不允许登录，非法登录", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 2:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "登录超时", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 3:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "该账号已经登陆,请换其他号！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 4:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "check不对！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 11:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "该账号存在非法操作，已禁用！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 12:
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "异常错误！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						default:
						{
							break;
						}
					}
//					this.sendNotification(CLZNoticeName.SHOW_FULL_TIP, {tip: "连接成功", isShowBtn: false, btnName: "", strNoticeName: ""});
//					this.sendNotification(CLZNoticeName.HIDE_FULL_TIP);
//					this.sendNotification(CLZNoticeName.SHOW_TOP);
					break;
				}
				case CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED:
				{
					var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "无法连接网络！", tipTitle: "网络提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
					this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
					break;
				}
				case CLZNoticeName.CONNECT_SOCKET_SUCC:
				{
					if (!this.mcLogin.btn_Login.hasEventListener(MouseEvent.CLICK))
					this.mcLogin.btn_Login.addEventListener(MouseEvent.CLICK, this.onLoginHandler);
					break;
				}
				default:
				{
					break;
				}
			}
		}

		public override function onRegister():void
		{
			//初始化界面以及handler
			this.mcLogin.btn_Login.addEventListener(MouseEvent.CLICK, this.onLoginHandler);
			//通知连接网络
//			if (!GameData.isConnectSocket)
//				this.sendNotification(CLZNoticeName.SOCKETCOMMAND_CONNECT_SOCKET);
		}

		private function onLoginHandler(event:MouseEvent):void
		{
			this.mcLogin.btn_Login.removeEventListener(MouseEvent.CLICK, this.onLoginHandler);
			userid=this.mcLogin.txt_userid.text;
			this.sendNotification(CLZNoticeName.SOCKETCOMMAND_REQ_LOGIN, {userid: this.mcLogin.txt_userid.text, password: this.mcLogin.txt_password.text,key:GameData.key});
		}

		public function get mcLogin():MovieClip
		{
			return viewComponent as MovieClip;
		}
	}

}
